package org.gitman.agentplatform.agent.impl.random;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.gitman.agentplatform.agent.api.AgentInfo;
import org.gitman.agentplatform.agent.api.AgentPlan;
import org.gitman.agentplatform.agent.api.utils.AbstractAgent;
import org.gitman.agentplatform.model.Vertex;

public class RandomAgent extends AbstractAgent {
	private Random rand;
	
	@Override
	public void init(String args) {
		rand = new Random(Calendar.getInstance().getTimeInMillis());
		super.init(args);
	}
	@Override
	public Integer getStep(Collection<AgentInfo> agentsInRange) {
		Integer step = agentInfo.getCurrentVertexID();
		Vertex v = graph.getVertexByID(step);
		ArrayList<Vertex> list = new ArrayList<Vertex>(v.getNeighbours().size());
		Set<Integer> set = new HashSet<Integer>();
		for(AgentInfo info : agentsInRange){
			set.add(info.getCurrentVertexID());
		}
		for(Vertex n : v.getNeighbours()){
			if(!set.contains(n)){
				list.add(n);
			}
		}		
		if(list.size() >0){
			int id = rand.nextInt(list.size());
			step = list.get(id).getId();
		}
		
		return step;
	}
	@Override
	public AgentPlan getPlan(int depth, Collection<AgentInfo> agentsInRange) {
		return null;
	}

}
